#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;

int main() {
  Mat src = imread("../images/2.jpg");
  if (src.empty()) {
    cout << "could not load image.." << endl;
    return -1;
  }
  namedWindow("opencv setup demo", CV_WINDOW_AUTOSIZE);
  imshow("opencv setup demo", src);

  Mat dst1, dst2;
  char src_title[] = "sourceimage";
  char dst_title1[] = "image1";
  char dst_title2[] = "image2";

  GaussianBlur(src, dst1, Size(3, 3), 0, 0);
  Mat gray_src;
  cvtColor(dst1, gray_src, CV_BGR2GRAY);
  // imshow("gray image", gray_src);

  Mat xgray, ygray;
  Sobel(gray_src, xgray, CV_16S, 1, 0, 3);
  Sobel(gray_src, ygray, CV_16S, 0, 1, 3);
  convertScaleAbs(xgray, xgray);
  convertScaleAbs(xgray, ygray);
  // imshow("xgray", xgray);
  // imshow("ygray", ygray);

  // Mat xygray;
  // addWeighted(xgray, 0.5, ygray, 0.5, 0, xygray);
  // imshow("xygray", xygray);

  Mat xygray = Mat(xgray.size(), xgray.type());
  int width = xgray.cols;
  int height = ygray.rows;
  for (int row = 0; row < height; row++) {
    for (int col = 0; col < width; col++) {
      int xg = xgray.at<char>(row, col);
      int yg = ygray.at<char>(row, col);
      int xy = xg + yg;
      xygray.at<char>(row, col) = saturate_cast<uchar>(xy);
    }
  }
  imshow("xygray", xygray);

  waitKey(0);
  return 0;
}